package com.atguigu.demojuc.chap05;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author njg
 * @version 1.0
 * @date 2025/6/17 15:51
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3,
                () -> System.out.println("所有玩家完成关卡，即将进入下一关"));
        for (int i = 1; i <= 3; i++) {
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + "\t" + "玩家完成第一关挑战");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread().getName() + "\t" + "玩家完成第二关挑战");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread().getName() + "\t" + "玩家完成第三关挑战");
                    cyclicBarrier.await();
                } catch (Exception e ) {
                    e.printStackTrace();
                }
            }, "玩家"+i).start();
        }
    }
}
